// pages/user/personal/index.js
import requestUtil from '../../../baseRequest'
import urls from '../../../requestUrl'

Page({

    /**
     * 页面的初始数据
     */
    data: {
        userInfo: {},
        driverRegisterPath: '/pages/user/memberRegister/before',
        driverDetailPath: '/pages/user/detailDriver/index',
        leaderRegisterPath: '/pages/user/registerLeader/index',
        leaderDetailPath: '/pages/user/detailLeader/index',
        serviceRegisterPath: '/pages/user/registerService/index',
        serviceDetailPath: '/pages/user/detailService/index',
        insuranceRegisterPath: '/pages/user/registerInsurance/index',
        insuranceDetailPath: '/pages/user/detailnsurance/index',
        personalDetailPath: '/pages/user/detailPersonal/index',
        kfActions: [
            // {
            //     name: '在线客服',
            //     icon: 'emoji_fill',
            //     openType: 'contact',
            // },
            {
                name: '客服热线',
                icon: 'customerservice_fill',
            },
            {
                name: '取消'
            },
        ],
        showKFAction: false
    },

    /**用户授权登陆 */
    handleGetUserInfo({
        detail
    }) {
        if (detail.userInfo) {
            this.updateUserInfo(detail.userInfo)
            wx.navigateTo({
                url: '/pages/user/memberRegister/before',
            });
        } else {
            wx.showModal({
                title: '登陆失败',
                content: '授权登陆失败，无法获取您的公开信息(昵称，头像等),请重新授权登陆',
                showCancel: false
            })
        }
    },

    /**更新用户授权信息 */
    updateUserInfo(info) {
        console.log(info)
        let param = {
            "headimgUrl": info.avatarUrl,
            "nickname": info.nickName,
            "openid": this.data.userInfo.openid
        }
        requestUtil.postRequest(urls.user.updateInfo, param, (res) => {
            if (res && (res.code == 10014)) {
                getApp().userLogin(() => {
                    this.setData({
                        ['userInfo.headimgUrl']: info.avatarUrl,
                        ['userInfo.nickname']: info.nickName,
                        ['userInfo.isMember']: 1
                    })
                    getApp().updateGlobalUserInfo(1, info.avatarUrl, info.nickName)
                    requestUtil.postRequest(urls.user.updateInfo, param, (res) => {
                        wx.showToast({
                            icon: 'success',
                            title: '登陆成功'
                        })
                    })
                })
            } else {
                this.setData({
                    ['userInfo.headimgUrl']: info.avatarUrl,
                    ['userInfo.nickname']: info.nickName,
                    ['userInfo.isMember']: 1
                })
                getApp().updateGlobalUserInfo(1, info.avatarUrl, info.nickName)
                wx.showToast({
                    icon: 'success',
                    title: '登陆成功'
                })
            }
        }, () => {})
    },

    /**拦截处理列表点击事件 */
    handleLinkCheck({
        currentTarget
    }) {
        console.log('handleLinkCheck', currentTarget)
        if (this.data.userInfo.isRegister == 0) {
            wx.showModal({
                title: '温馨提示',
                content: '您尚未完成用户注册操作，请完成完成注册后重新操作',
                showCancel: false,
                confirmText: '立即注册',
                success: res => {
                    if (res.confirm) {
                        wx.navigateTo({
                            url: '/pages/user/registerUser/index'
                        })
                    }
                }
            })
            return
        }
        let url = currentTarget.dataset.url
        wx.navigateTo({
            url: url
        })
    },
    /**处理点击“在线客服”功能*/
    handleToCustomerService(e) {
        this.setData({
            showKFAction: true
        })
    },
    /**处理客服 */
    handleClickContactAction(e) {
        let _index = e.detail.index+1
        switch (_index) {
            case 0:
                this.setData({
                    showKFAction: false
                })
                break;
            case 1:
                wx.makePhoneCall({
                    phoneNumber: this.data.userInfo.customerServiceLine || '13413162721',
                    success: () => {
                        this.setData({
                            showKFAction: false
                        })
                    }
                })
                break;
            case 2:
                this.setData({
                    showKFAction: false
                })
                break;
        }
    },
    refreshUserInfo(){
        console.log('个人页签加载')
        let openid = wx.getStorageSync('openid');
        if(!openid){
            wx.login({
                success: res => {
                    requestUtil.getRequest(urls.user.getCode + res.code, {}, (res) => {
                        console.log('success', res)
                        wx.setStorageSync('openid', res.openid)
                        wx.setStorageSync("isMember", res.is_member)
                        this.refreshUserInfo();
                    });
                }
            })
        }else{
            getApp().getUserInfo(openid, () => {
                let userInfo = getApp().globalData.userInfo
                console.log('userinfo刷新')
                console.log(userInfo);
                this.setData({
                    userInfo: userInfo
                })
            })
        }
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        let _distributeCode = options.scene
        if (_distributeCode) {
            getApp().saveDistributeCode(_distributeCode);
        }
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        this.refreshUserInfo();
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {
        getApp().getUserInfo(getApp().getUserOpenid(), () => {
            let userInfo = getApp().globalData.userInfo
            console.log('onPullDownRefresh:', userInfo)
            this.setData({
                userInfo: userInfo
            })
            wx.stopPullDownRefresh()
        })
    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})